import { defineStore } from "pinia";
import { getSingUrl, getAlList, getSingDetail,getTrackList } from "@/api/Player";
import convertMilliseconds from "@/hooks/convertMilliseconds";
let playerStore = defineStore('playerStore', {
    state: () => {
        return {
            flag: true,
            singMessage: [],
            singTime: {
                minutes: convertMilliseconds(0).minutes,
                seconds: convertMilliseconds(0).seconds
            },
            singUrl: '',
            alSongList: [],
            index: 0
        }
    },
    actions: {
        async getsingMessage(id: any) {
            let resultDetail = await getSingDetail(id)
            if (resultDetail.code == 200) {
                this.singMessage = resultDetail.songs[0]
            }
            let result = await getSingUrl(id)
            if (result.code == 200) {
                this.flag = false
                this.singUrl = result.data[0].url
            }
        },
        async getSingAl(id: number,limit?:number) {
            if(!limit){
                let result = await getAlList(id)
                this.index=0
                if (result.code == 200) {
                    this.alSongList = result.songs
                    this.getsingMessage(this.alSongList[this.index]['id'])
                }
            }else{
                let result = await getTrackList(id,limit)
                this.index=0
                if (result.code == 200) {
                    this.alSongList = result.songs
                    this.getsingMessage(this.alSongList[this.index]['id'])
                }
            }
        }
    }
})

export default playerStore